<?php

namespace app\admin\model;

use app\admin\common\ModelBase;
use service\JsonService;
use service\TreeService;
use service\UtilService;
use think\Db;
use think\Exception;

class Visitor extends ModelBase
{

    protected $update = ["update_time"];


    /**
     * @return mixed
     * @author: hhygyl
     * @name: setUpdateTimeAttr
     * @describe:
     */
    protected function setUpdateTimeAttr()
    {
        return time();
    }



    /**
     * @param array $param
     * @param string $order
     * @author: hhygyl <hhygyl520@qq.com>
     * @name: GetModelDataPage
     * @describe:获取分页数据
     */
    public function GetVisitorsDataPage($param = [], $order = "v.id desc")
    {
        $where = [];
        if(!empty($param)) {
            //搜索条件
            if(!empty($param['name'])) {
                $where['visitoruname|visitorphone'] = ['like', "%".$param['name']."%"];
            }
            //日期查询
            if (!empty($param['stime']) && empty($param['etime'])) {
                $where['v.visitorstime'] = [['egt', strtotime($param['stime'] . " 00:00:00")], ['elt', strtotime($param['stime']. " 23:59:59")]];
            } elseif (empty($param['stime']) && !empty($param['etime'])) {
                $where['v.visitorstime'] = [['egt', strtotime($param['etime'] . " 00:00:00")], ['elt', strtotime($param['etime']. " 23:59:59")]];
            } elseif (!empty($param['stime']) && !empty($param['etime'])) {
                $where['v.visitorstime'] = [['egt', strtotime($param['stime']. " 00:00:00")], ['elt', strtotime($param['etime']. " 23:59:59")]];
            }
        }

        try {
            $fields = "v.*,u.username";
            $data = Db::name("visitor")->alias("v")
                ->join("so_user u","u.user_id = v.user_id","left")
                ->where($where)
                ->field($fields)
                ->order($order)->page(PAGE)->limit(PERPAGE)->select();
            //dump($data);exit;
            //echo $this->getLastSql();exit;
            $ctime = time();
            foreach ($data as &$row){
                $row['visitor_state'] = '邀约中';
                if($row['visitoretime'] < $ctime || $row['state'] == 2){
                    $row['visitor_state'] = '已失效';
                }
                $row['visitorstime_str'] = date("Y-m-d H:i:s",$row['visitorstime']);
                $row['visitoretime_str'] = date("Y-m-d H:i:s",$row['visitoretime']);
                $row['vsendmsg_state_str'] = $row['sendmsg_state'] == 1 ? "发送成功" : "发送失败";
            }
            $count = Db::name("visitor")->alias("v")
                ->join("so_user u","u.user_id = v.user_id","left")
                ->where($where)
                ->field($fields)
                ->order($order)->count("v.id");
        } catch (Exception $exception) {

            return JsonService::fail($exception->getMessage());
        }
        return JsonService::result(0, "", $data, $count);

    }


    /**
     * @param $params
     * @return false|int
     * @author: hhygyl
     * @name: editData
     * @describe:修改数据
     */
    public function editData($params)
    {
        $res = $this->isUpdate(true)->allowField(true)->save($params);
        return $res;
    }

}